<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Village' );

global $dal, $premium;

$query2 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
if( isset( $row2[ 'faction_id' ] ) ) { 
	$tmp_faction_id = $row2[ 'faction_id' ];
	$query2 = "SELECT farm_support, coal_support, iron_support FROM faction WHERE faction_id = " . $tmp_faction_id;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$farm_support = $row2[ 'farm_support' ];
	$coal_support = $row2[ 'coal_support' ];
	$iron_support = $row2[ 'iron_support' ];
}
else {
	$farm_support = 0;
	$coal_support = 0;
	$iron_support = 0;
	$tmp_faction_id = -1;
}
//include_once( "../global/profiler.inc" );

//$prof = new Profiler( true ); // Output the profile information but no trace
//$prof->startTimer( "village_stats" );

$village_name = formatInput( $_GET[ 'name' ] );

$query = "SELECT * " .
		"FROM village " .
		"WHERE name = '" . $village_name . "'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	$village_id = $row[ "village_id" ];
	$capital = $row[ 'start_place' ];
	$loyalty = $row[ 'loyalty' ];
	$loyal_to = $row[ 'loyal_to' ];
	$sendWorkersTo = $row[ 'send_workers_to' ];
	$sendWorkersAmount = $row[ 'send_workers_amount' ];

	$farmingPrefered = $row[ 'food_jobs_prefered' ];
	$farmingCurrent = $row[ 'food_jobs' ];
	$ironPrefered = $row[ 'iron_jobs_prefered' ];
	$ironCurrent = $row[ 'iron_jobs' ];
	$coalPrefered = $row[ 'coal_jobs_prefered' ];
	$coalCurrent = $row[ 'coal_jobs' ];

	$focus = $row[ 'focus' ];
	$automatic = $row[ 'automatic' ];
	
	$_SESSION[ 'village_id' ] = $village_id;
	
	include( "../global/resources.php" );
	$query2 = "SELECT morale, gold FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$morale = $row2[ 'morale' ];	
	$gold = $row2[ 'gold' ];	
?>
					<br />
					<table id="blank_dark" class="four-parts-table">
<?php
	$query = "SELECT * " .
			"FROM village " .
			"WHERE name = '" . addslashes( $village_name ) . "'";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if( $row[ 'start_place' ] == 1 ) {
		$title = translate( 'Capital' );
	}
	else {
		if( $row[ 'population' ] < 5000 ) {
			$title = translate( 'Outpost' );
		}
		else if( $row[ 'population' ] < 10000 ) {
			$title = translate( 'Settlement' );
		}
		else if( $row[ 'population' ] < 50000 ) {
			$title = translate( 'Village' );
		}
		else if( $row[ 'population' ] < 150000 ) {
			$title = translate( 'Town' );
		}
		else if( $row[ 'population' ] < 500000 ) {
			$title = translate( 'City' );
		}
		else {
			$title = translate( 'Metropolis' );
		}
	}

	$houses = getMaxPopulation( $row[ 'village_id' ]);
?>
						<tr>
							<td><strong><?php print( $title ); ?></strong></td>
							<td align="right"><a href="village.php?name=<?php print( $village_name ); ?>"><strong><?php print( $village_name ); ?></strong></a></td>
							<td><?php transprint( 'Location' ); ?></td>
							<td align="right"><a href="../operations/map.php?x=<?php print( $row[ 'x' ] - 20 ); ?>&y=<?php print( $row[ 'y' ] - 20 ); ?>">(<?php print( $row[ 'x' ] ); ?>, <?php print( $row[ 'y' ] ); ?>)</a></td>
						</tr>
						<tr>
							<td><?php transprint( 'Population' ); ?></td>
							<td align="right"><strong class="population"><?php print( number_format( $row[ 'population' ] ) ); ?></strong></td>
							<td><?php transprint( 'Houses' ); ?></td>
							<td align="right">
<?php
	if( $loyalty == 100 && $loyal_to == $_SESSION[ 'ruler_id' ] ) {
?>								<form method="post" action="change_max_population.php">
									<input type="hidden" name="max_population" value="<?php print( $houses ); ?>" />
									<input type="hidden" name="village_id" value="<?php print( $row[ 'village_id' ] ); ?>" />
									<input type="submit" value="<?php transprint( 'set' ); ?>" title="<?php transprint( 'Set maximum population to available houses' ); ?>" />
								</form>
<?php
	}
?>
								<strong class="houses"><?php print( number_format( $houses ) ); ?></strong>
							</td>
						</tr>
<?php
	$workforce = $row[ 'workforce' ];
	$jobsOccupied = $row[ 'jobs' ];
	
	if( $workforce - $jobsOccupied > 0 ) {
		$unemploymentPercent = round( ( $workforce - $jobsOccupied ) / $workforce * 100, 2 );
	}
	else {
		$unemploymentPercent = 0;
	}

	if( $loyalty == 100 && $loyal_to == $_SESSION[ 'ruler_id' ] ) {
?>
						<tr>
							<td><?php transprint( 'Maximum population' ); ?></td>
							<td align="right">
								<form method="post" action="change_max_population.php">
									<input type="textfield" size="10" name="max_population" value="<?php print( $row[ 'max_population' ] ); ?>" />
									<input type="hidden" name="village_id" value="<?php print( $row[ 'village_id' ] ); ?>" />
									<input type="submit" value="<?php transprint( 'change' ); ?>" />
								</form>
							</td>
							<td><?php transprint( 'Workers' ); ?></td>
							<td align="right"><strong class="population"><?php print( number_format( $workforce ) ); ?></strong></td>
						</tr>
<?php
	}
	else {
?>
						<tr>
							<td><?php transprint( 'Maximum population' ); ?></td>
							<td align="right"><?php print( number_format( $row[ 'max_population' ] ) ); ?></td>
							<td><?php transprint( 'Available workers' ); ?></td>
							<td align="right"><strong class="population"><?php print( number_format( $workforce ) ); ?></strong></td>
						</tr>
<?php
	}
?>
						<tr>
							<td><?php transprint( 'Unemployment' ); ?></td>
							<td align="right"><strong class="population"><?php print( number_format( $workforce - $jobsOccupied >= 1 ? $workforce - $jobsOccupied : 0 ) ); ?></strong> <a href="javascript:void(null);" onclick="AOS.workersDialog.show();"><?php print( $unemploymentPercent > 20 ? '<strong class="red">' : '' ); ?>(<?php print( $unemploymentPercent ); ?>%)<?php print( $unemploymentPercent > 20 ? '</strong>' : '' ); ?></a></td>
							<td><?php transprint( 'Jobs occupied' ); ?></td>
							<td align="right"><strong class="jobs"><?php print( number_format( $jobsOccupied ) ); ?></strong></td>
						</tr>
<?php
	$food_income = $row[ 'food_income' ];
	if( $food_income < 0 ) {
		$suffix = "-";
	}
	else {
		$suffix = '';
	}
	if( $farm_support == 1 ) {
		$food_extra = $row[ 'food_quality' ] * 0.1;
	}
	else {
		$food_extra = 0;
	}
	if( $coal_support == 1 ) {
		$coal_extra = $row[ 'coal_quality' ] * 0.1;
	}
	else {
		$coal_extra = 0;
	}
	if( $iron_support == 1 ) {
		$iron_extra = $row[ 'iron_quality' ] * 0.1;
	}
	else {
		$iron_extra = 0;
	}
	if( $focus == 'housing' ) {
		$coal_extra -= $row[ 'coal_quality' ] * 0.1;
		$iron_extra -= $row[ 'iron_quality' ] * 0.1;
	}
	elseif( $focus == 'farming' ) {
		$food_extra += $row[ 'food_quality' ] * 0.1;
		$coal_extra -= $row[ 'coal_quality' ] * 0.1;
		$iron_extra -= $row[ 'iron_quality' ] * 0.1;
	}
	elseif( $focus == 'iron' ) {
		$food_extra -= $row[ 'food_quality' ] * 0.1;
		$iron_extra += $row[ 'iron_quality' ] * 0.1;
	}
	elseif( $focus == 'coal' ) {
		$food_extra -= $row[ 'food_quality' ] * 0.1;
		$coal_extra += $row[ 'coal_quality' ] * 0.1;
	}
	
	$query2 = "SELECT count(*) " .
			"FROM village_buildings " .
			"WHERE building_id = 15 AND village_id = " . $village_id;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( $row2[ 'count(*)' ] > 0 ) {
		$food_extra = round( $row[ 'food_quality' ] * 0.5 + $food_extra );
	}
	
	if( $morale <= 0.2 ) {
		$food_extra -= round( ( $row[ 'food_quality' ] + $food_extra ) * 0.5 ); 
	}
	else if( $morale <= 0.4 ) {
		$food_extra -= round( ( $row[ 'food_quality' ] + $food_extra ) * 0.2 ); 
	}
	
	if( $food_extra > 0 ) {
		$added = " (+" . round( $food_extra ) . ")";
	}
	elseif( $food_extra < 0 ) {
		$added = " (" . round( $food_extra ) . ")";
	}
	else {
		$added = '';
	}
?>
						<tr>
							<td><?php transprint( 'Food production' ); ?></td>
							<td align="right"><strong class="food"><?php print( $suffix . number_format( $food_income ) ); ?></strong></td>
							<td><?php transprint( 'quality' ); ?></td>
							<td align="right"><strong class="food"><?php print( $row[ 'food_quality' ] . $added ); ?></strong></td>
						</tr>
<?php
	$query2 = "SELECT count(*) FROM village_buildings WHERE building_id = 17 AND village_id = " . $village_id;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( $row2[ 'count(*)' ] > 0 ) {
		$iron_extra = round( $row[ 'iron_quality' ] * 0.5 + $iron_extra );
	}
	
	if( $morale <= 0.2 ) {
		$iron_extra -= round( ( $row[ 'iron_quality' ] + $iron_extra ) * 0.5 ); 
	}
	else if( $morale <= 0.4 ) {
		$iron_extra -= round( ( $row[ 'iron_quality' ] + $iron_extra ) * 0.2 ); 
	}
	
	if( $iron_extra > 0 ) {
		$added = " (+" . round( $iron_extra ) . ")";
	}
	elseif( $iron_extra < 0 ) {
		$added = " (" . round( $iron_extra ) . ")";
	}
	else {
		$added = "";
	}
	print "<tr><td>" . translate( 'Iron extraction' ) . "</td><td align=\"right\"><strong class=\"iron\">" . number_format( $row[ 'iron_income' ] ) . "</strong></td><td>" . translate( 'quality' ) . "</td><td align=\"right\"><strong class=\"iron\">" . $row[ 'iron_quality' ] . "$added</strong></td></tr>\r\n";
	$query2 = "SELECT count(*) FROM village_buildings WHERE building_id = 19 AND village_id = " . $village_id;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( $row2[ 'count(*)' ] > 0 ) {
		$coal_extra = round( $row[ 'coal_quality' ] * 0.5 + $coal_extra );
	}
	
	if( $morale <= 0.2 ) {
		$coal_extra -= round( ( $row[ 'coal_quality' ] + $coal_extra ) * 0.5 ); 
	}
	elseif( $morale <= 0.4 ) {
		$coal_extra -= round( ( $row[ 'coal_quality' ] + $coal_extra ) * 0.2 ); 
	}
	
	if( $coal_extra > 0 ) {
		$added = " (+" . round( $coal_extra ) . ")";
	}
	elseif( $coal_extra < 0 ) {
		$added = " (" . round( $coal_extra ) . ")";
	}
	else {
		$added = "";
	}
?>
						<tr>
							<td><?php transprint( 'Coal extraction' ); ?></td>
							<td align="right"><strong class="coal"><?php print( number_format( $row[ 'coal_income' ] ) ); ?></strong></td>
							<td><?php transprint( 'quality' ); ?></td>
							<td align="right"><strong class="coal"><?php print( $row[ 'coal_quality' ] . $added ); ?></strong></td>
						</tr>
						<tr>
							<td><?php transprint( 'Space available' ); ?></td>
							<td align="right"><strong class="space-available"><?php print( $row[ 'space' ] ); ?></strong></td>
<?php
	$query2 = "SELECT building_id, ammount " .
			"FROM village_buildings " .
			"WHERE village_id = " . $row[ 'village_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$num2 = mysql_numrows( $result2 );
	$space_used = 0;
	$p = 0;
	while ( $p < $num2 ) {
		$result3 = mysql_query( "SELECT space FROM buildings WHERE building_id = " . mysql_result( $result2, $p, "building_id" )  ) or die ( "Query failed: " . mysql_error() );
		$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
		if( $row3[ 'space' ] != -1 ) {
			$space_used += mysql_result( $result2, $p, "ammount" ) * $row3[ 'space' ];
		}
		$p ++;
	}

	$result2 = mysql_query( "SELECT building_id FROM village_queue WHERE village_id = " . $row[ 'village_id' ] . " AND rank = 0 AND time > 0" ) or die ( "Query failed: " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$building_id = $row2[ 'building_id' ];
	if( isset( $building_id ) ) {
		$result2 = mysql_query( "SELECT space FROM buildings WHERE building_id = " . $building_id ) or die ( "Query failed: " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$space_used += $row2[ 'space' ];
	}
?>
							<td><?php transprint( 'Space used' ); ?></td>
							<td align="right"><strong class="space-used"><?php print( $space_used ); ?></strong></td>
						</tr>
<?php	
	if( $_SESSION[ 'ruler_id' ] == $row[ 'loyal_to' ] ) {
		$loyal_towards = '<strong class="green">';
	}
	else {
		$allied = false;
		$query2 = "SELECT faction_id " .
				"FROM faction_member " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		if( isset( $row2[ 'faction_id' ] ) ) {
			$faction_id = $row2[ 'faction_id' ];
			$query2 = "SELECT faction_id " .
					"FROM faction_member " .
					"WHERE ruler_id = " . $row[ 'loyal_to' ] . " AND invited = 0 AND requested = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			if( isset( $row2[ 'faction_id' ] ) && $row2[ 'faction_id' ] == $faction_id ) {
				$allied = true;
			}
		}
		
		if( $allied ) {
			$loyal_towards = '<strong class="blue">';
		}
		else {
			$loyal_towards = '<strong class="red">';
		}
	}
	
	$query2 = "SELECT name " .
			"FROM ruler " .
			"WHERE ruler_id = " . $row[ 'loyal_to' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( isset( $row2[ 'name' ] ) ) {
		$loyal_towards = '<a href="../overview/ruler.php?name=' . $row2[ 'name' ] . '">' . $loyal_towards . $row2[ 'name' ] . '</strong></a>';
	}
	else {
		$loyal_towards = '<strong class="brown">' . translate( 'Independents' ) . '</strong>';
	}
	print( "<tr><td>" . translate( 'Loyalty' ) . "</td><td align=\"right\">" . $row[ 'loyalty' ] . '%</td><td>' . translate( 'Loyal towards' ) . '</td><td align="right">' . $loyal_towards . '</td></tr>' );
	
	$query2 = "SELECT SUM(defense_bonus), SUM(offense_bonus) FROM village_buildings JOIN buildings ON village_buildings.building_id = buildings.building_id WHERE village_id = " . $village_id;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	
	if( $row2[ 'SUM(defense_bonus)' ] != 0 || $row2[ 'SUM(offense_bonus)' ] != 0 ) {
?>
						<tr>
							<td><?php transprint( 'Defense bonus' ); ?></td>
							<td align="right"><strong class="defense-bonus"><?php print( round( $row2[ 'SUM(defense_bonus)' ] * 100 ) ); ?>%</strong></td>
							<td><?php transprint( 'Damage bonus' ); ?></td>
							<td align="right"><strong class="offense-bonus"><?php print( round( $row2[ 'SUM(offense_bonus)' ] * 100 ) ); ?>%</strong></td>
						</tr>
<?php
	}
	
	$query = "SELECT village_id, name " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION['ruler_id'] . " " .
			"ORDER BY name";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	if( $num > 1 ) {
		$multipleVillages = true;
		$i = 0;
		while ( $i < $num ) {
			if( mysql_result( $result, $i, "village_id" ) == $village_id ) {
				if( $i > 0 ) {
					print( "<tr><td><a href=\"village.php?name=" . mysql_result( $result, ( $i - 1 ), "name" ) . "\">" . translate( 'Previous' ) . "</a></td><td>&nbsp;</td>" );
				}
				else {
					print( "<tr><td>&nbsp;</td><td>&nbsp;</td>" );
				}
				if( $i < $num - 1 ) {
					print( "<td>&nbsp;</td><td align=\"right\"><a href=\"village.php?name=" . mysql_result( $result, ( $i + 1 ), "name" ) . "\">" . translate( 'Next' ) . "</a></td></td></tr>\r\n" );
				}
				else {
					print( "<td>&nbsp;</td><td>&nbsp;</td></tr>\r\n" );
				}
			}
			$i ++;
		}
	}
	else {
		$multipleVillages = false;
	}
?>
					</table>
					<br />
<?php
	//$prof->stopTimer( "village_stats" );
	$statement = $dal->prepare( 'SELECT show_village_image ' .
			'FROM users ' .
			'WHERE user_id = :user_id' );
	$statement->bindParam( ':user_id', $_SESSION[ 'user_id' ], PDO::PARAM_INT );		
	$showVillageImage = $dal->getValue( $statement );
	if( $showVillageImage == 1 ) {
		printVillageMap( $village_id );
	}
?>
						<div class="blok">
							<a href="javascript:void" onclick="AOS.queConstructionDialog.show();"><strong><?php transprint( 'Schedule construction' ); ?></strong></a>
							&nbsp;|&nbsp;<a href="javascript:void" onclick="AOS.completedBuildingDialog.show();"><strong><?php transprint( 'Completed buildings' ); ?></strong></a>
							&nbsp;|&nbsp;<a href="javascript:void" onclick="AOS.workersDialog.show();"><strong><?php transprint( 'Workers' ); ?></strong></a>
<?php
	$query2 = "SELECT loyalty, loyal_to " .
			"FROM village " .
			"WHERE village_id = $village_id";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	
	if( $capital == 0 && $row2[ 'loyalty' ] == 100 && $row2[ 'loyal_to' ] == $_SESSION[ 'ruler_id' ] ) {
?>
							&nbsp;|&nbsp;<a href="javascript:void" onclick="AOS.otherOptionsDialog.show();"><strong><?php transprint( 'Other options' ); ?></strong></a>
<?php
	}
?>
						</div>
<?php

	if( isset( $_SESSION[ 'que_building_status' ] ) ) {
?>
						<br />
						<div class="blok">
							<?php transprint( 'Status' ); ?>: <?php print( $_SESSION[ 'que_building_status' ] ); ?>
						</div>
<?php
		unset( $_SESSION[ 'que_building_status' ] );
	}
	

	//$prof->startTimer( "buildings" );
	
	$query = "SELECT village_buildings.building_id, ammount, buildings.name, buildings.food_income, buildings.coal_income, buildings.iron_income, buildings.space, buildings.jobs " .
			"FROM village_buildings " .
			"JOIN buildings ON buildings.building_id = village_buildings.building_id " .
			"WHERE village_id = '" . $village_id . "' " .
			"ORDER BY food_income DESC, " .
				"coal_income DESC, " .
				"iron_income DESC, " .
				"housing DESC, " .
				"space DESC, " .
				"name";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );

	$statement = $dal->prepare( 'SELECT DISTINCT building_id ' .
			'FROM village_buildings ' .
			'WHERE village_id = :village_id' );
	$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
	$buildingsPresent = $dal->getArrayOfObjects( $statement );
	$building_id = -1;
	$statement = $dal->prepare( 'SELECT building_id ' .
			'FROM buildings ' .
			'WHERE requires = :building_id' );
	$statement->bindParam( ':building_id', $building_id, PDO::PARAM_INT );
?>
					<div id="completed-building-dialog" class="hidden">
						<div class="hd"><?php transprint( 'Completed buildings' ); ?></div>
						<div class="bd">
							<form method="post" action="remove_building.php">
								<input type="hidden" value="<?php print( $village_id ); ?>" name="village_id" />
								<table>
									<tr>
										<td><?php transprint( 'Name' ); ?></td>
										<td align="right"><?php transprint( 'Food production' ); ?></td>
										<td align="right"><?php transprint( 'Coal extraction' ); ?></td>
										<td align="right"><?php transprint( 'Iron extraction' ); ?></td>
										<td align="right"><?php transprint( 'Space' ); ?></td>
										<td align="right"><?php transprint( 'Jobs' ); ?></td>
										<td align="right"><?php transprint( 'Amount' ); ?></td>
										<td colspan="2" align="left"><?php transprint( 'Remove' ); ?></td>
									<tr/>
<?php	
	$i = 0;
	while ( $i < $num ) {
		$ammount = mysql_result( $result, $i, "ammount" );
?>
								<tr<?php print( $i % 2 == 0 ? ' class="even"' : '' ); ?>>
<?php
		$building_id = mysql_result( $result, $i, 'building_id' );
		$allowRemoval = false;
		if( $building_id != 0 
				&& $loyal_to == $_SESSION[ 'ruler_id' ] && $loyalty > 50 
				&& mysql_result( $result, $i, 'space' ) > 0 ) {
			$dependingOn = $dal->getArrayOfObjects( $statement );
			$foundOne = false;
			foreach( $dependingOn AS $building ) {
				foreach( $buildingsPresent AS $buildingPresent ) {
					if( $building->building_id == $buildingPresent->building_id ) {
						$foundOne = true;
						break 2;
					}
				}
			}
			if( !$foundOne ) {
				$allowRemoval = true;
			}
		}
		
		$tooltip = getBuildingTooltip( $building_id );

		$food_tmp = number_format( mysql_result( $result, $i, 'food_income' ) * $ammount );
		if( $food_tmp == "0" ) {
			$food_tmp = "";
		}
		else {
			$food_tmp = "<strong class=\"food\">" . $food_tmp . "</strong>";
		}
		$coal_tmp = number_format( mysql_result( $result, $i, 'coal_income' ) * $ammount );
		if( $coal_tmp == "0" ) {
			$coal_tmp = "";
		}
		else {
			$coal_tmp = "<strong class=\"coal\">" . $coal_tmp . "</strong>";
		}
		$iron_tmp = number_format( mysql_result( $result, $i, 'iron_income' ) * $ammount );
		if( $iron_tmp == "0" ) {
			$iron_tmp = "";
		}
		else {
			$iron_tmp = "<strong class=\"iron\">" . $iron_tmp . "</strong>";
		}
?>
												<td><a href="building.php?id=<?php print( mysql_result( $result, $i, "building_id" ) ); ?>" title="<?php print( $tooltip ); ?>" class="make-yui-tooltip"><?php transprint( mysql_result( $result, $i, 'name' ) ); ?></a></td>
												<td align="right"><?php print( $food_tmp ); ?></td>
												<td align="right"><?php print( $coal_tmp ); ?></td>
												<td align="right"><?php print( $iron_tmp ); ?></td>
												<td align="right"><?php print( mysql_result( $result, $i, 'space' ) > 0 ? '<strong class="space-used">' . ( mysql_result( $result, $i, 'space' ) * $ammount ) . '</strong>' : '<strong class="space-available">' . ( mysql_result( $result, $i, 'space' ) * $ammount * -1 ) . '</strong>' ); ?></td>
												<td align="right"><?php print( mysql_result( $result, $i, 'jobs' ) > 0 ? '<strong class="jobs">' . number_format( mysql_result( $result, $i, 'jobs' ) * $ammount ) . '</strong>' : '' ); ?></td>
												<td align="right"><?php print( number_format( $ammount ) ); ?></td>
												<td align="right">
<?php
		if( $allowRemoval ) {
?>													
													<input type="checkbox" value="remove" name="remove_<?php print( $building_id ); ?>" />
<?php
		}
?>													
												</td>
												<td align="right">
<?php
		if( $allowRemoval ) {
?>													
													<input type="text" size="2" name="remove_amount_<?php print( $building_id ); ?>" value="1" />
<?php
		}
?>													
												</td>
											</tr>
<?php
		$i ++;
	}
?>
										</table>
										<input type="submit" value="<?php transprint( 'Add remove orders to queue' ); ?>" />
									</form>
								</div>
								<a href="javascript:void( null )" onclick="AOS.completedBuildingDialog.hide();"><span class="container-close"> </span></a>
							</div>
							<br />
<?php
	//$prof->stopTimer( "buildings" );

	//$prof->startTimer( "building_queue" );
	$query = "SELECT village_queue.*, buildings.name, buildings.construction_jobs, buildings.ammount_allowed " .
			"FROM village_queue " .
			"JOIN buildings ON buildings.building_id = village_queue.building_id " .
			"WHERE village_id = " . $village_id . " " .
			"ORDER BY rank";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );

	if( $num > 0 ) {
		$village_queue = true;
?>
							<table>
								<tr>
									<th colspan="6"><?php transprint( 'Construction queue' ); ?></th>
								</tr>
								<tr>
									<td align="right"><?php transprint( 'Order' ); ?></td>
									<td><?php transprint( 'Name' ); ?></td>
									<td><?php transprint( 'Repeat' ); ?></td>
									<td align="right"><?php transprint( 'Time' ); ?></td>
									<td align="right"><?php transprint( 'Jobs' ); ?></td>
									<td align="right"><?php transprint( 'Remove' ); ?></td>
								</tr>
<?php
		$i = 0;
		while ( $i < $num ) {
			$jobs = mysql_result( $result, $i, 'construction_jobs' );
			$amountAllowed = mysql_result( $result, $i, 'ammount_allowed' );
?>
								<tr<?php print( $i % 2 == 0 ? ' class="even"' : '' ); ?>>
<?php

			$tooltip = getBuildingTooltip( mysql_result( $result, $i, 'building_id' ) );
			
			if( mysql_result( $result, $i, 'destroy' ) == 1 ) {
				$name = translate( 'Destroy' ) . " <a href=\"building.php?id=" . mysql_result( $result, $i, "building_id" ) . "\" title=\"$tooltip\" class=\"make-yui-tooltip\">" . translate( mysql_result( $result, $i, 'name' ) ) . "</a>";
				// destroying a building doesn't take jobs for gameplay reasons
				// (when you are short of workers you need someway to fix it, only way is by destroying buildings which occupy workers)
				$jobs = 0;
			}
			else {
				$name = "<a href=\"building.php?id=" . mysql_result( $result, $i, "building_id" ) . "\" title=\"$tooltip\" class=\"make-yui-tooltip\">" . translate( mysql_result( $result, $i, 'name' ) ) . "</a>";
			}

			$rank = "";
			if( mysql_result( $result, $i, "rank" ) > 0 ) {
				$rank = mysql_result( $result, $i, "rank" );
			}
			$time = mysql_result( $result, $i, "time" );
			$remove = "";
			if( $time < 0 ) {
				$time = "";
				$remove = "<a href=\"delete_queue.php?rank=" . mysql_result( $result, $i, "rank" ) . "&village_id=$village_id\"><img src=\"../resources/delete.png\" title=\"" . translate( 'delete from queue' ) . "\" alt=\"" . translate( 'delete from queue' ) . "\" border=\"0\"></a>";
			}
			
?>			
									<td align="right"><?php print( $rank ); ?></td>
									<td><?php print( $name ); ?></td>
									<td>
<?php 
			if( $amountAllowed == -1 && mysql_result( $result, $i, 'destroy' ) == 0 ) {
?>
										<form method="post" action="toggle_repeat.php">
											<input type="hidden" value="<?php print( $village_id ); ?>" name="village_id" />
											<input type="hidden" value="<?php print( mysql_result( $result, $i, 'village_queue_id' ) ); ?>" name="village_queue_id" />
											<select name="repeat" onchange="this.parentNode.submit();">
												<option value="no"<?php print( mysql_result( $result, $i, 'repeat' ) == 0 ? ' selected="selected"' : '' ) ?>><?php transprint( 'Don\'t repeat' ); ?></option>
												<option value="yes"<?php print( mysql_result( $result, $i, 'repeat' ) == 1 ? ' selected="selected"' : '' ) ?>><?php transprint( 'Repeat' ); ?></option>
											</select>
										</form>
<?php
			}
?>
									</td>
									<td align="right"><?php print( $time ); ?></td>
									<td align="right"><?php print( $jobs > 0 ? '<strong class="jobs">' . number_format( $jobs ) . '</strong>' : '' ); ?></td>
									<td align="right"><?php print( $remove ); ?></td>
								</tr>
<?php
			$i ++;
		}
?>	
							</table>
<?php
	}
	else {
?>
						<div class="blok">
							<?php transprint( 'There are no buildings being constructed or buildings waiting for construction.' ); ?>
						</div>
<?php
		$village_queue = false;
	}

	//$prof->stopTimer( "building_queue" );

	//$prof->startTimer( "to_build" );
	$result2 = mysql_query( "SELECT count(*) FROM village_queue WHERE village_id = " . $village_id ) or die ( "Query failed: " . mysql_error() );
	$row = mysql_fetch_array( $result2, MYSQL_ASSOC );

?>	
					<div id="que-construction-dialog" class="hidden">
						<div class="hd"><?php transprint( 'Build' ); ?></div>
						<div class="bd">
<?php	
	if( $row[ 'count(*)' ] < getQueueLength() ) {
		$query = "SELECT building_id, requires, upgrade, ammount_allowed, " .
				"food_income, coal_income, iron_income, gold_cost, name, " .
				"time_cost, space, construction_jobs " .
				"FROM buildings " .
				"ORDER BY food_income DESC, " .
					"coal_income DESC, " .
					"iron_income DESC, " .
					"housing DESC, " .
					"space DESC, " .
					"name";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
?>
							<form action="que_building.php" method="post">
								<table>
									<tr>
										<td><?php transprint( 'Build' ); ?></td>
										<td><?php transprint( 'Amount' ); ?></td>
										<td><?php transprint( 'Repeat' ); ?></td>
										<td><?php transprint( 'Name' ); ?></td>
										<td align="right"><?php transprint( 'Gold cost' ); ?></td>
										<td align="right"><?php transprint( 'Build time' ); ?></td>
										<td align="right"><?php transprint( 'Food production' ); ?></td>
										<td align="right"><?php transprint( 'Coal extraction' ); ?></td>
										<td align="right"><?php transprint( 'Iron extraction' ); ?></td>
										<td align="right"><?php transprint( 'Space' ); ?></td>
										<td align="right"><?php transprint( 'Construction Jobs' ); ?></td>
									</tr>
<?php
		$i = 0;
		$count = 0;
		while ( $i < $num ) {
			//$prof->startTimer( "building_" . $i );
			$show = true;
			$requires = mysql_result( $result, $i, "requires" );
			if( $requires != -1 ) {
				//echo $requires;
				$show = false;
				$result2 = mysql_query( "SELECT count(*) " .
						"FROM village_buildings " .
						"WHERE building_id = " . $requires . " AND village_id = " . $village_id ) or die ( "Query failed: " . mysql_error() );
				$row = mysql_fetch_array($result2, MYSQL_ASSOC);
				if( $row[ 'count(*)' ] > 0 ) {
					//echo " true ";
					$show = true;
				}
				else {
					//echo " false ";
					$result2 = mysql_query( "SELECT count(*) " .
							"FROM village_queue " .
							"WHERE building_id = " . $requires . " AND village_id = " . $village_id ) or die ( "Query failed: " . mysql_error() );
					$row = mysql_fetch_array($result2, MYSQL_ASSOC);
					if( $row[ 'count(*)' ] > 0 ) {
						$show = true;
					}
				}
			}
			$allowed = mysql_result( $result, $i, "ammount_allowed" );
			if( $show && $allowed != -1 ) {
				if( $village_queue ) {
					$result2 = mysql_query( "SELECT count(*) FROM village_buildings " .
						"WHERE building_id = " . mysql_result( $result, $i, "building_id" ) . " AND village_id = " . $village_id ) or die ( "Query failed: " . mysql_error() );
					$row = mysql_fetch_array($result2, MYSQL_ASSOC);
					$total_count = $row[ 'count(*)' ];
					
					$result2 = mysql_query( "SELECT count(*) FROM village_queue " .
						"WHERE building_id = " . mysql_result( $result, $i, "building_id" ) . " AND village_id = " . $village_id ) or die ( "Query failed: " . mysql_error() );
					$row = mysql_fetch_array($result2, MYSQL_ASSOC);
					
					$total_count = $total_count + $row[ 'count(*)' ];
				}
				else {
					$result2 = mysql_query( "SELECT count(*) FROM village_buildings " .
						"WHERE building_id = " . mysql_result( $result, $i, "building_id" ) . " AND village_id = " . $village_id ) or die ( "Query failed: " . mysql_error() );
					$row = mysql_fetch_array($result2, MYSQL_ASSOC);
					$total_count = $row[ 'count(*)' ];
				}
				if( $total_count < $allowed ) {
					$show = true;
				}
				else {
					$show = false;
				}
			}
			$upgrade = mysql_result( $result, $i, "upgrade" );
			if( $show && $upgrade != -1 ) {
				$result2 = mysql_query( "SELECT count(*) FROM upgrade_status " .
					"WHERE upgrade_id = " . $upgrade . " AND ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND achieved = 1" ) or die ( "Query failed: " . mysql_error() );
				$row = mysql_fetch_array($result2, MYSQL_ASSOC);
				if( $row[ 'count(*)' ] == 0 ) {
					$show = false;
				}
			}
	
			if( $show ) {
?>
									<tr<?php print( $count % 2 == 0 ? ' class="even"' : '' ); ?>>
<?php
				$food_tmp = mysql_result( $result, $i, "food_income" );
				if( $food_tmp == "0" ) {
					$food_tmp = "";
				}
				else {
					$food_tmp = "<strong class=\"food\">" . $food_tmp . "</strong>";
				}
				$coal_tmp = mysql_result( $result, $i, "coal_income" );
				if( $coal_tmp == "0" ) {
					$coal_tmp = "";
				}
				else {
					$coal_tmp = "<strong class=\"coal\">" . $coal_tmp . "</strong>";
				}
				$iron_tmp = mysql_result( $result, $i, "iron_income" );
				if( $iron_tmp == "0" ) {
					$iron_tmp = "";
				}
				else {
					$iron_tmp = "<strong class=\"iron\">" . $iron_tmp . "</strong>";
				}
				$tooltip = getBuildingTooltip( mysql_result( $result, $i, 'building_id' ) );
?>
										<td><input type="checkbox" onclick="fillField( this.name )" name="check_<?php print( mysql_result( $result, $i, "building_id" ) ); ?>" /></td>
										<td><input type="text" size="1" name="text_<?php print( mysql_result( $result, $i, "building_id" ) ); ?>" id="<?php print( mysql_result( $result, $i, "building_id" ) ); ?>" /></td>
										<td>
											<?php if( mysql_result( $result, $i, 'ammount_allowed' ) == -1 ) { ?>
											<input type="checkbox" value="1" name="repeat_<?php print( mysql_result( $result, $i, "building_id" ) ); ?>" />
											<?php } ?>
										</td>
										<td><a href="building.php?id=<?php print( mysql_result( $result, $i, "building_id" ) ); ?>" title="<?php print( $tooltip ); ?>" class="make-yui-tooltip"><?php transprint( mysql_result( $result, $i, "name" ) ); ?></a></td>
										<td align="right"><?php print( mysql_result( $result, $i, 'gold_cost' ) > 0 ? '<strong class="gold">' . mysql_result( $result, $i, 'gold_cost' ) . '</strong>' : '' ); ?></td>
										<td align="right"><?php print( mysql_result( $result, $i, "time_cost" ) ); ?></td>
										<td align="right"><?php print( $food_tmp ); ?></td>
										<td align="right"><?php print( $coal_tmp ); ?></td>
										<td align="right"><?php print( $iron_tmp ); ?></td>
										<td align="right"><?php print( mysql_result( $result, $i, 'space' ) > 0 ? '<strong class="space-used">' . mysql_result( $result, $i, 'space' ) . '</strong>' : '<strong class="space-available">' . ( mysql_result( $result, $i, 'space' ) * -1 ) . '</strong>' ); ?></td>
										<td align="right"><strong class="jobs"><?php print( mysql_result( $result, $i, "construction_jobs" ) ); ?></strong></td>
									</tr>
<?php
				$count ++;
			}
			//$prof->stopTimer( "building_" . $i );
			$i ++;
		}
?>
								</table>
								<input type="submit" value="<?php transprint( 'Start construction' ); ?>" />
								<input type="hidden" name="village_id" value="<?php print( $village_id ); ?>" />
							</form>
<?php
	}
	else {
?>
							<?php transprint( 'Maximum number of buildings queued.' ); ?>
<?php		
	}
?>
						</div>
						<a href="javascript:void( null )" onclick="AOS.queConstructionDialog.hide();"><span class="container-close"> </span></a>
					</div>
<?php	
	$statement = $dal->prepare( 'SELECT SUM( ammount * jobs ) ' .
			'FROM village_buildings ' .
			'JOIN buildings ON buildings.building_id = village_buildings.building_id ' .
			'WHERE food_income = 0 AND iron_income = 0 AND coal_income = 0 AND village_id = :village_id' );
	$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
	$otherJobs = $dal->getValue( $statement );
	$statement = $dal->prepare( 'SELECT construction_jobs ' .
			'FROM village_queue ' .
			'JOIN buildings ON buildings.building_id = village_queue.building_id ' .
			'WHERE time != 0 AND village_id = :village_id' );
	$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
	$otherJobs += $dal->getValue( $statement );
	if( $otherJobs > $workforce ) {
		$otherJobs = $workforce;
	}

	$statement = $dal->prepare( 'SELECT SUM( ammount * jobs ) ' .
			'FROM village_buildings ' .
			'JOIN buildings ON buildings.building_id = village_buildings.building_id ' .
			'WHERE food_income > 0 AND village_id = :village_id' );
	$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
	$farmingMax = $dal->getValue( $statement );

	$statement = $dal->prepare( 'SELECT SUM( ammount * jobs ) ' .
			'FROM village_buildings ' .
			'JOIN buildings ON buildings.building_id = village_buildings.building_id ' .
			'WHERE iron_income > 0 AND village_id = :village_id' );
	$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
	$ironMax = $dal->getValue( $statement );

	$statement = $dal->prepare( 'SELECT SUM( ammount * jobs ) ' .
			'FROM village_buildings ' .
			'JOIN buildings ON buildings.building_id = village_buildings.building_id ' .
			'WHERE coal_income > 0 AND village_id = :village_id' );
	$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
	$coalMax = $dal->getValue( $statement );
?>	
					<div id="workers-dialog" class="hidden">
						<div class="hd"><?php transprint( 'Workers' ); ?></div>
						<div class="bd">
							<form method="post" action="update_workers.php">
								<div class="worker-group">
									<strong><?php transprint( 'Employment overview' ); ?></strong><br />
									<?php transprint( 'Workers' ); ?>: <strong class="population"><?php print( number_format( $workforce ) ); ?></strong><br />
									<?php transprint( 'Jobs occupied' ); ?>: <strong class="jobs"><?php print( number_format( $jobsOccupied ) ); ?></strong><br />
									<?php transprint( 'Unemployed' ); ?>: <strong class="population"><?php print( number_format( $workforce - $jobsOccupied ) ); ?></strong><br />
									<?php transprint( 'Jobs occupied outside production' ); ?>: <strong class="jobs"><?php print( number_format( $otherJobs ) ); ?></strong><br />
<?php									
	if( $premium > 0 ) {
?>
									<input type="checkbox" name="automatic" value="automatic"<?php print( $automatic == 1 ? ' checked="checked"' : '' ); ?> id="automatic-workers" /> <label for="automatic-workers"><?php transprint( 'Auto assign workers' ); ?></label> <a href="../aosmanual/concepts.php#automatic worker assignment"><small>(<?php transprint( 'explain' ); ?>)</small></a><br />
<?php		
	}
	else {
?>
									<input type="hidden" name="automatic" value="<?php print( $automatic ); ?>" />
<?php		
	}
	$statement = $dal->prepare( 'SELECT SUM(send_workers_amount) ' .
			'FROM village ' .
			'WHERE send_workers_to = :send_workers_to AND ruler_id = :ruler_id' );
	$statement->bindParam( ':send_workers_to', $village_id, PDO::PARAM_INT );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$incomingWorkers = $dal->getValue( $statement );
	if( $incomingWorkers > 1 ) {
?>
									<?php transprint( 'Workers from other villages' ); ?>: <strong class="population"><?php print( number_format( floor( $incomingWorkers ) ) ); ?></strong><br /> 
									<?php transprint( 'Workers left after 50% transfer penalty' ); ?>: <strong class="population"><?php print( number_format( floor( $incomingWorkers * 0.5 ) ) ); ?></strong><br /> 
<?php
	}
	
	if( $multipleVillages ) {
		$maxSendWorkers = $workforce - $jobsOccupied + $sendWorkersAmount;		
?>
									<?php transprint( 'Put' ); ?> <input id="send-workers-amount-textfield" type="text" name="transfer_number" size="6" value="<?php print( $sendWorkersAmount ); ?>" /> (<a href="javascript:void(null)" onclick="document.getElementById( 'send-workers-amount-textfield' ).value = <?php print( $maxSendWorkers ); ?>;"><?php transprint( 'max' ); ?></a>) <?php transprint( 'workers to work in' ); ?> <select name="transfer_to">
										<option value="-1"><?php transprint( 'nowhere' ); ?></option>
<?php
		$statement = $dal->prepare( 'SELECT name, village_id ' .
				'FROM village ' .
				'WHERE ruler_id = :ruler_id AND village_id != :village_id ' .
				'ORDER BY name' );
		$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
		$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
		$villages = $dal->getArrayOfObjects( $statement );
		foreach( $villages AS $village ) {
?>
										<option value="<?php print( $village->village_id ); ?>"<?php print( $sendWorkersTo == $village->village_id ? ' selected="selected"' : '' ); ?>><?php print( $village->name ); ?></option>
<?php
		}
?>
									</select> <small>(<a href="../aosmanual/concepts.php#Transfer workers"><?php transprint( 'explain' ); ?></a>)</small><br />
<?php		
	}
	else {
		
	}
?>
									<input type="radio" name="focus"<?php print( $focus == 'no' ? ' checked="checked"' : '' ); ?> value="no" id="no-focus" /> <label for="no-focus"><?php transprint( 'No focus' ); ?></label><br />
									<?php transprint( 'No effect' ); ?>
								</div>
								<div class="worker-group">
									<strong><?php transprint( 'Houses' ); ?></strong><br />
									<input type="radio" name="focus"<?php print( $focus == 'housing' ? ' checked="checked"' : '' ); ?> value="housing" id="housing-focus" /> <strong class="houses"><label for="housing-focus"><?php transprint( 'Focus on houses' ); ?></label></strong><br />
									<strong class="houses">+10% <?php transprint( 'houses' ); ?></strong>, <strong class="iron">-10% <?php transprint( 'iron' ); ?></strong> <?php transprint( 'and' ); ?> <strong class="coal">-10% <?php transprint( 'coal' ); ?></strong><br /> 
								</div>
								<div class="worker-group">
									<strong><?php transprint( 'Farming' ); ?></strong><br />
									<input type="radio" name="focus"<?php print( $focus == 'farming' ? ' checked="checked"' : '' ); ?> value="farming" id="farming-focus" /> <strong class="food"><label for="farming-focus"><?php transprint( 'Focus on farming' ); ?></label></strong><br />
									<strong class="food">+10% <?php transprint( 'food' ); ?></strong>, <strong class="iron">-10% <?php transprint( 'iron' ); ?></strong> <?php transprint( 'and' ); ?> <strong class="coal">-10% <?php transprint( 'coal' ); ?></strong><br /> 
									<?php transprint( 'Workers' ); ?>: <strong class="population"><?php print( number_format( $farmingCurrent ) ); ?></strong><br />
									<?php transprint( 'Maximum workers' ); ?>: <strong class="population"><?php print( number_format( $farmingMax ) ); ?></strong><br />
<?php
	if( $premium > 0 && $automatic == 1 ) {
?>
									<input type="hidden" name="farming" value="<?php print( $farmingPrefered ); ?>" />
<?php
	}
	else {
?>
									<?php transprint( 'Prefered workers' ); ?>: <input type="text" size="20" name="farming" value="<?php print( $farmingPrefered ); ?>" /><br />
<?php
	}
?>
								</div>
								<div class="worker-group">
									<strong><?php transprint( 'Iron mining' ); ?></strong><br />
									<input type="radio" name="focus"<?php print( $focus == 'iron' ? ' checked="checked"' : '' ); ?> value="iron" id="iron-focus" /> <strong class="iron"><label for="iron-focus"><?php transprint( 'Focus on iron mining' ); ?></label></strong><br />
									<strong class="iron">+10% <?php transprint( 'iron' ); ?></strong>, <strong class="houses">-10% <?php transprint( 'houses' ); ?></strong> <?php transprint( 'and' ); ?> <strong class="food">-10% <?php transprint( 'food' ); ?></strong><br />
									<?php transprint( 'Workers' ); ?>: <strong class="population"><?php print( number_format( $ironCurrent ) ); ?></strong><br />
									<?php transprint( 'Maximum workers' ); ?>: <strong class="population"><?php print( number_format( $ironMax ) ); ?></strong><br />
<?php
	if( $premium > 0 && $automatic == 1 ) {
?>
									<input type="hidden" name="iron" value="<?php print( $ironPrefered ); ?>" />
<?php
	}
	else {
?>
									<?php transprint( 'Prefered workers' ); ?>: <input type="text" size="20" name="iron" value="<?php print( $ironPrefered ); ?>" /><br />
<?php
	}
?>
								</div>
								<div class="worker-group">
									<strong><?php transprint( 'Coal mining' ); ?></strong><br />
									<input type="radio" name="focus"<?php print( $focus == 'coal' ? ' checked="checked"' : '' ); ?> value="coal" id="coal-focus" /> <strong class="coal"><label for="coal-focus"><?php transprint( 'Focus on coal mining' ); ?></label></strong><br />
									<strong class="coal">+10% <?php transprint( 'coal' ); ?></strong>, <strong class="houses">-10% <?php transprint( 'houses' ); ?></strong> <?php transprint( 'and' ); ?> <strong class="food">-10% <?php transprint( 'food' ); ?></strong><br /> 
									<?php transprint( 'Workers' ); ?>: <strong class="population"><?php print( number_format( $coalCurrent ) ); ?></strong><br />
									<?php transprint( 'Maximum workers' ); ?>: <strong class="population"><?php print( number_format( $coalMax ) ); ?></strong><br />
<?php
	if( $premium > 0 && $automatic == 1 ) {
?>
									<input type="hidden" name="coal" value="<?php print( $coalPrefered ); ?>" />
<?php
	}
	else {
?>
									<?php transprint( 'Prefered workers' ); ?>: <input type="text" size="20" name="coal" value="<?php print( $coalPrefered ); ?>" /><br />
<?php
	}
?>
								</div>
								<input type="hidden" name="village_id" value="<?php print( $village_id ); ?>" />
								<input type="submit" value="<?php transprint( 'Confirm' ); ?>" />
							</form>
						</div>
						<a href="javascript:void( null )" onclick="AOS.workersDialog.hide();"><span class="container-close"> </span></a>
					</div>
	

					<div id="other-options-dialog" class="hidden">
						<div class="hd"><?php transprint( 'Other options' ); ?></div>
						<div class="bd">
<?php	
	//$prof->stopTimer( "to_build" );
    //$prof->printTimers( true );
	$query2 = "SELECT loyalty, loyal_to " .
			"FROM village " .
			"WHERE village_id = $village_id";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	
	if( $capital == 0 && $row2[ 'loyalty' ] == 100 && $row2[ 'loyal_to' ] == $_SESSION[ 'ruler_id' ] ) {
		if( isset( $_SESSION[ 'drop_village_status' ] ) ) {
			print( "\t\t\t\t<div class=\"blok\">\r\n\t\t\t\t" . $_SESSION[ 'drop_village_status' ] . "\r\n\t\t\t\t</div><br />\r\n" );
			unset( $_SESSION[ 'drop_village_status' ] );
		}
?>
							<form method="post" action="drop_village.php">
								<div class="blok">
									<strong><?php transprint( 'Drop village' ); ?></strong><br />
									<input type="checkbox" name="drop" /> <?php transprint( 'I want to drop this village.' ); ?><br />
									<small>
										(<?php transprint( 'The village control will return to independents, all rulers will be able to conquer or bribe this village to get control of it.' ); ?>)
									</small><br />
									<input type="submit" name="drop_confirm" value="<?php transprint( 'Confirm' ); ?>" /><br />
									<input type="hidden" name="village_id" value="<?php print( $village_id ); ?>" />
								</div>
							</form>
<?php
		$query2 = "SELECT village_id, become_capital " .
				"FROM village " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND become_capital > -1";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
		if( !isset( $row2[ 'village_id' ] ) ) {
			if( $gold < 36000 ) {
?>
							<div class="blok">
								<?php transprint( 'You can\'t change your capital right now, it costs <strong class="gold">36,000</strong> gold.' ); ?><br />
							</div>
<?php
			}
			else {
				if( isset( $_SESSION[ 'make_capital_status' ] ) ) {
					print( "\t\t\t\t<div class=\"blok\">\r\n\t\t\t\t" . $_SESSION[ 'make_capital_status' ] . "\r\n\t\t\t\t</div><br />\r\n" );
					unset( $_SESSION[ 'make_capital_status' ] );
				}
?>
							<form method="post" action="make_capital.php">
								<div class="blok">
									<strong><?php transprint( 'Change capital' ); ?></strong><br />
									<?php transprint( 'Gold cost' ); ?>: <strong class="gold">36,000</strong><br />
									<?php transprint( 'Time: 48 days' ); ?><br />
									<input type="checkbox" name="change_capital" /> <?php transprint( 'I want to make this village my capital.' ); ?><br />
									<input type="submit" name="confirm" value="<?php transprint( 'Confirm' ); ?>" /><br />
									<input type="hidden" name="village_id" value="<?php print( $village_id ); ?>" />
								</div>
							</form>
<?php
			}
		}
		elseif( $village_id == $row2[ 'village_id' ] ) {
			$turn = getCurrentTurn();
			$complete = $row2[ 'become_capital' ];
			$complete = $complete - $turn;
			if( $complete == 1 ) {
				$complete = "1 " . translate( 'day' );
			}
			else {
				$complete = "$complete " . translate( 'days' );
			}
?>
							<br />
							<div class="blok">
								<?php transprint( 'This village is becoming your capital.' ); ?><br />
								<?php print( $complete ); ?> <?php transprint( 'until transfering the capital is complete.' ); ?>
							</div>
<?php
		}
		else {
?>
							<br />
							<div class="blok">
								<?php transprint( 'Another village is becoming your capital, you can make this village your capital when the previous change is done.' ); ?><br />
							</div>
<?php
		}
	}
?>
						</div>
						<a href="javascript:void( null )" onclick="AOS.otherOptionsDialog.hide();"><span class="container-close"> </span></a>
					</div>

<?php	

$added_code = "<script type=\"text/javascript\">\n" .
		"	function fillField( name ) {\n" .
		"		var tmp = document.getElementById( name.replace( \"check_\", \"\" ) );\n" .
		"		if( tmp && ( !tmp.value || tmp.value == \"\" ) ) {\n" .
		"			tmp.setAttribute( \"value\", \"1\" );\n" .
		"		}\n" .
		"	}\n" .
		"</script>\n" .
		"<script language=\"JavaScript\" type=\"text/javascript\">\n" .
		"	YAHOO.util.Event.addListener( window, 'load', initializeTooltips );\n" .
		"</script>";

aos_footer( $added_code );
}
else {
?>
					<?php transprint( 'You don\'t own this village!' ); ?><br />
<?php
aos_footer();
}
?>
